package com.atguigu.tingshu.user.api;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import me.chanjar.weixin.common.service.WxOAuth2Service;
import me.chanjar.weixin.common.service.WxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private RedisTemplate redisTemplate;

	/**
	 * 获取用户声音列表付费情况
	 * /api/user/userInfo/userIsPaidTrack/{userId}/{albumId}
	 * @param waitCheckTrackIdList
	 * @param userId
	 * @param albumId
	 * @return
	 */
	@PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
	Result<Map<Long, Integer>> userPaidTrack(@RequestBody List<Long> waitCheckTrackIdList,
											 @PathVariable Long userId,
											 @PathVariable Long albumId){
		Map<Long,Integer> resultMap=userInfoService.userPaidTrack(waitCheckTrackIdList,userId,albumId);

		return Result.ok(resultMap);
	}


	/**
	 * 根据用户ID查询用户信息
	 * /api/user/userInfo/getUserInfoVo/{userId}
	 * @param userId
	 * @return
	 */
	@GetMapping("userInfo/getUserInfoVo/{userId}")
	public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId){
		UserInfo userInfo = userInfoService.getById(userId);
		return Result.ok(BeanUtil.copyProperties(userInfo, UserInfoVo.class));
	}


	/**
	 * 更新用户信息
	 * /api/user/wxLogin/updateUser
	 * @param userInfoVo
	 * @param token
	 * @return
	 */
	@PostMapping("wxLogin/updateUser")
	public Result updateUser(@RequestBody UserInfoVo userInfoVo,@RequestHeader("token")String token){
		//获取请求中的token
		String tokenKey= RedisConstant.USER_LOGIN_KEY_PREFIX+token;
		//查redis 获取userinfoVo 获取userId
		UserInfoVo redisUserInfoVo = (UserInfoVo)redisTemplate.opsForValue().get(tokenKey);
		UserInfo userInfo = BeanUtil.copyProperties(userInfoVo, UserInfo.class);
		userInfo.setId(redisUserInfoVo.getId());
		userInfoService.updateById(userInfo);
		return Result.ok();
	}



}

